package com.bugu.web.customer;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;

import com.bugu.entity.customer.Company;
import com.bugu.entity.customer.Customer;
import com.bugu.entity.user.User;
import com.bugu.service.customer.CompanyManager;
import com.bugu.service.customer.CustomerManager;
import com.bugu.service.user.UserManager;
@SuppressWarnings("serial")
@Results( { @Result(name = CRUDActionSupport.RELOAD, location = "customer", type = "redirect") })
public class CustomerAction extends CRUDActionSupport<Customer> {
	
	Logger logger = LoggerFactory.getLogger(CustomerAction.class);
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void prepareInput() throws Exception {
		logger.debug("prepare input");
		loginName = SpringSecurityUtils.getCurrentUserName();
		logger.debug("loginName:"+loginName);
		/**
		 * 提取下拉列表
		 */
		Page<Company> page = new Page<Company>();
		page = companyManager.findByUser(page, loginName);
		companyList = page.getResult();
	}
	@Override
	public String input() throws Exception {
		return INPUT;
	}
	public void prepareDetail() throws Exception {
		logger.debug("prepare detail");
		loginName = SpringSecurityUtils.getCurrentUserName();
		logger.debug("loginName:"+loginName);
	}
	public String detail() throws Exception {
		
		logger.debug(String.valueOf(companyId));
		page = customerManager.findCustomerByCompanyAndUser(page, companyId,
				loginName);
		logger.debug(page.getResult().get(0).getName());

		return SUCCESS;
	}

	@Override
	public String list() throws Exception {
		logger.debug("prepare list");
		loginName = SpringSecurityUtils.getCurrentUserName();
		logger.debug("loginName:"+loginName);
		page = customerManager.findCustomerByCompany(page, loginName);
		return SUCCESS;
	}
	@Override
	public void prepare() throws Exception {

	}
	@Override
	protected void prepareModel() throws Exception {
	
		if(id!=null){
			entity = customerManager.get(id);
		}else{
			entity = new Customer();
		}
		
	}
	/*@Override
	public void prepareSave(){
		logger.debug("prepare save");
		loginName = SpringSecurityUtils.getCurrentUserName();
		logger.debug("loginName:"+loginName);
		User user = userManager.findByLoginName(loginName);
		logger.debug(user.getLoginName());
		Company company = companyManager.get(companyId);
		logger.debug(company.getName());
		logger.debug(entity.getName());
		entity.setUser(user);
		entity.setCompany(company);
		
	}*/
	@Override
	public String save() throws Exception {
		loginName = SpringSecurityUtils.getCurrentUserName();
		logger.debug("loginName:"+loginName);
		User user = userManager.findByLoginName(loginName);
		logger.debug(user.getLoginName());
		Company company = companyManager.get(companyId);
		logger.debug(company.getName());
		logger.debug(entity.getName());
		entity.setUser(user);
		entity.setCompany(company);
		
		customerManager.save(entity);
		return RELOAD;
	}

	public Customer getModel() {

		return entity;
	}
	
	/**
	 * 以下是不太关注的东西
	 */
	private Page<Customer> page = new Page<Customer>(10);
	private Customer entity = null;
	private Long id = null;
	private Long companyId = null;
	private String loginName = null;
	
	
	/**
	 * 下拉列表
	 */
	private List<Company> companyList = null;
	

	@Autowired
	private CustomerManager customerManager = null;
	@Autowired
	private CompanyManager companyManager = null;
	@Autowired
	private UserManager userManager = null;
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CustomerManager getCustomerManager() {
		return customerManager;
	}

	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}

	public void setEntity(Customer entity) {
		this.entity = entity;
	}
	public Page<Customer> getPage() {
		return page;
	}

	public void setPage(Page<Customer> page) {
		this.page = page;
	}
	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}
	public List<Company> getCompanyList() {
		return companyList;
	}
	public void setCompanyList(List<Company> companyList) {
		this.companyList = companyList;
	}
	public CompanyManager getCompanyManager() {
		return companyManager;
	}
	public void setCompanyManager(CompanyManager companyManager) {
		this.companyManager = companyManager;
	}

}
